<?php
ini_set('error_reporting', 6143);
ini_set('display_errors','1');
define('IN_LC', true);
define('MM_ID', 1);
ini_set('wincache.fcenabled','0');

include_once("validate.php");
include_once("includes/style.php");
include_once("includes/addMessage.php");
include_once("includes/side_menu.php");


//first we need to validate if we are an admin
if(isset($_SESSION['valid_id']))
{
	if(!(  $_SESSION["valid_admin"] > 1))
	{	
		Header("Location: index.php");
	}
}
else
{
	Header("Location: index.php");
}

$style = new style(true);

$style->addRenderLeft(new side_menu('admin'));

// editing championships?
if(isset($_GET['action']) && $_GET['action'] == 'championships')
{
    include_once("admin/championships.php");
    $style->addRenderRight(new adminChampionships());
}
// editing series?
else if(isset($_GET['action']) && $_GET['action'] == 'series')
{
    include_once("admin/series.php");
    $style->addRenderRight(new adminSeries());
}
// editing admins?
else if(isset($_GET['action']) && $_GET['action'] == 'champclass')
{
	include_once("admin/edit_champclasses.php");
	include_once("admin/edit_block.php");
	$style->addRenderRight(new addMessage($lang['AdminEditChampClassesInfo'],$lang['AdminWelcomeTitle']));
	if(isset($_POST['itemID']))
	{
		$style->addRenderRight(new edit_block('champclass',  'Championships', 
										  'SELECT c_id, c_title FROM ' . CHAMPIONSHIPS_TABLE . ' ORDER BY c_title',
										  'c_id', 'c_title', '', false, $_POST['itemID']));
		$style->addRenderRight(new edit_champclasses($_POST['itemID']));
	}
	else
	{
	
		$style->addRenderRight(new edit_block('champclass',  'Championships', 
										  'SELECT c_id, c_title FROM ' . CHAMPIONSHIPS_TABLE . ' ORDER BY c_title',
										  'c_id', 'c_title', '', false));
	}
	
}
// editing tracks?
else if(isset($_GET['action']) && $_GET['action'] == 'tracks')
{
    include_once("admin/tracks.php");
    $style->addRenderRight(new adminTracks());
}
// editing faq cats?
else if(isset($_GET['action']) && $_GET['action'] == 'faqcatagories')
{
	include_once("admin/edit_block.php");
	include_once("admin/edit_faqcatagories.php");
	$style->addRenderRight(new addMessage($lang['AdminEditFAQCatagoryInfo'],$lang['AdminWelcomeTitle']));
	$style->addRenderRight(new edit_block('faqcatagories', 'FAQCatagory', 
										  'SELECT fc_id, fc_name FROM ' . FAQCATAGORIES_TABLE . ' ORDER BY fc_name',
										  'fc_id', 'fc_name'));
	$style->addRenderRight(new edit_faqcatagories());
}
// editing faqs?
else if(isset($_GET['action']) && $_GET['action'] == 'faqs')
{
	include_once("admin/edit_block.php");
	include_once("admin/edit_faqs.php");
	$style->addRenderRight(new addMessage($lang['AdminEditFAQInfo'],$lang['AdminWelcomeTitle']));
	$style->addRenderRight(new edit_block('faqs', 'FAQ', 
										  'SELECT fq_id, CONCAT(fc_name, " - ", fq_question) as title FROM ' . FAQS_TABLE . ', ' . FAQCATAGORIES_TABLE . 
										  ' WHERE fq_catagory = fc_id ORDER BY fc_name ASC, fq_question ASC',
										  'fq_id', 'title'));
	$style->addRenderRight(new edit_faqs());
}
// editing teams?
else if(isset($_GET['action']) && $_GET['action'] == 'teams')
{
	include_once("admin/edit_block.php");
	include_once("admin/edit_team.php");
	$style->addRenderRight(new addMessage($lang['AdminEditTeamsInfo'],$lang['AdminWelcomeTitle']));
	$style->addRenderRight(new edit_block('teams', 'Teams', 
										  'SELECT t_id, t_name FROM ' . TEAMS_TABLE . ' ORDER BY t_name',
										  't_id', 't_name'));
	$style->addRenderRight(new edit_team());
}
// editing news articles?
else if(isset($_GET['action']) && $_GET['action'] == 'newsarticles')
{
	include_once("admin/edit_block.php");
	include_once("admin/edit_news.php");
	$style->addRenderRight(new addMessage($lang['AdminEditTeamsInfo'],$lang['AdminWelcomeTitle']));
	$style->addRenderRight(new edit_block('newsarticles', 'AdminEditNewsArticles', 
										  'SELECT n_id, n_title FROM ' . NEWSARTICLES_TABLE . ' ORDER BY n_title',
										  'n_id', 'n_title'));
	$style->addRenderRight(new edit_news());
}
// editing champ signups?
else if(isset($_GET['action']) && $_GET['action'] == 'champsignups')
{
	include_once("admin/edit_block.php");
	include_once("admin/champsignups.php");
	$style->addRenderRight(new addMessage($lang['AdminEditChampSignupsInfo'],$lang['AdminWelcomeTitle']));
	if(isset($_POST['itemID']))
	{
		$style->addRenderRight(new edit_block('champsignups',  'Championships', 
										  'SELECT c_id, c_title FROM ' . CHAMPIONSHIPS_TABLE . ' ORDER BY c_title',
										  'c_id', 'c_title', '', false, $_POST['itemID']));
		$style->addRenderRight(new champsignups($_POST['itemID']));
	}
	else
	{
		$style->addRenderRight(new edit_block('champsignups',  'Championships', 
										  'SELECT c_id, c_title FROM ' . CHAMPIONSHIPS_TABLE . ' ORDER BY c_title',
										  'c_id', 'c_title', '', false));
	}
}
// editing champ signups?
else if(isset($_GET['action']) && $_GET['action'] == 'drivername')
{
	
	if(!(  $_SESSION["valid_admin"] > 2))
	{	
		Header("Location: index.php");
	}
	
	include_once("admin/edit_block.php");
	include_once("admin/edit_drivername.php");
	$style->addRenderRight(new addMessage($lang['AdminEditDrivernameInfo'],$lang['AdminWelcomeTitle']));
	if(isset($_POST['itemID']))
	{
		$style->addRenderRight(new edit_block('drivername',  'Drivers', 
										  'SELECT u.user_id,
										  		  CONCAT(u.username, ": ", pf_drivername) AS drivername 
												  FROM ' . USERS_TABLE . ' u, '. USERSGROUPS_TABLE . ' g, '  . DRIVERNAME_TABLE . ' d 
												  WHERE g.user_id = u.user_id AND g.group_id = 3 AND d.user_id = u.user_id ORDER BY drivername',
										  'user_id', 'drivername', '', false, $_POST['itemID']));
		$style->addRenderRight(new edit_drivername($_POST['itemID']));
	}
	else
	{
		$style->addRenderRight(new edit_block('drivername',  'Drivers', 
										  'SELECT u.user_id,
										  		  CONCAT(u.username, ": ", pf_drivername) AS drivername 
										   FROM ' . USERS_TABLE . ' u, '. USERSGROUPS_TABLE . ' g, '  . DRIVERNAME_TABLE . ' d 
										   WHERE g.user_id = u.user_id AND g.group_id = 3 AND d.user_id = u.user_id ORDER BY drivername',
										  'user_id', 'drivername', '', false));
	}
}
// editing event signups?
else if(isset($_GET['action']) && $_GET['action'] == 'eventsignups')
{
	include_once("admin/edit_block.php");
	include_once("admin/eventsignups.php");
	$style->addRenderRight(new addMessage($lang['AdminEditChampSignupsInfo'],$lang['AdminWelcomeTitle']));
	if(isset($_POST['itemID']))
	{
		$style->addRenderRight(new edit_block('eventsignups',  'Events', 
					"SELECT  DISTINCT e_id, CONCAT(c_smallTitle, ' ', e_title, ' ', t_fullTitle) AS e_name FROM " 
					. EVENTDATES_TABLE . ", " . EVENTS_TABLE .", " . CHAMPIONSHIPS_TABLE .  ", " . TRACKS_TABLE . 
					" WHERE e_track = t_id AND ed_event = e_id AND e_championship = c_id ORDER BY ed_date ASC",
										  'e_id', 'e_name', '', false, $_POST['itemID']));
		$style->addRenderRight(new eventsignups($_POST['itemID']));
	}
	else
	{
		$style->addRenderRight(new edit_block('eventsignups',  'Events', 
					"SELECT DISTINCT e_id, CONCAT(c_smallTitle, ' ', e_title, ' ', t_fullTitle) AS e_name FROM " 
					. EVENTDATES_TABLE . ", " . EVENTS_TABLE .", " . CHAMPIONSHIPS_TABLE .  ", " . TRACKS_TABLE . 
					" WHERE e_track = t_id AND ed_event = e_id AND e_championship = c_id ORDER BY ed_date ASC",
										  'e_id', 'e_name', '', false));
	}
}
// editing champ reserved numbers?
else if(isset($_GET['action']) && $_GET['action'] == 'reservednumbers')
{
	include_once("admin/edit_block.php");
	include_once("admin/reservednumbers.php");
	$style->addRenderRight(new addMessage($lang['AdminEditReservedNumbersInfo'],$lang['AdminWelcomeTitle']));
	if(isset($_POST['itemID']))
	{
		$style->addRenderRight(new edit_block('reservednumbers',  'Championships', 
										  'SELECT c_id, c_title FROM ' . CHAMPIONSHIPS_TABLE . ' ORDER BY c_title',
										  'c_id', 'c_title', '', false, $_POST['itemID']));
		$style->addRenderRight(new reservednumbers($_POST['itemID']));
	}
	else
	{
		$style->addRenderRight(new edit_block('reservednumbers',  'Championships', 
										  'SELECT c_id, c_title FROM ' . CHAMPIONSHIPS_TABLE . ' ORDER BY c_title',
										  'c_id', 'c_title', '', false));
	}
}
// editing incident judges?
else if(isset($_GET['action']) && $_GET['action'] == 'judges')
{
	include_once("admin/edit_block.php");
	include_once("admin/edit_judges.php");
	$style->addRenderRight(new addMessage($lang['AdminEditJudgesInfo'],$lang['AdminWelcomeTitle']));
	if(isset($_POST['itemID']))
	{
		$style->addRenderRight(new edit_block('judges',  'Championships', 
										  'SELECT c_id, c_title FROM ' . CHAMPIONSHIPS_TABLE . ' ORDER BY c_title',
										  'c_id', 'c_title', '', false, $_POST['itemID']));
		$style->addRenderRight(new edit_judges());
	}
	else
	{
		$style->addRenderRight(new edit_block('judges',  'Championships', 
										  'SELECT c_id, c_title FROM ' . CHAMPIONSHIPS_TABLE . ' ORDER BY c_title',
										  'c_id', 'c_title', '', false));
	}
}
// editing champ reserved divisions?
else if(isset($_GET['action']) && $_GET['action'] == 'reserveddivisions')
{
	include_once("admin/edit_block.php");
	include_once("admin/reserveddivisions.php");
	$style->addRenderRight(new addMessage($lang['AdminEditReservedDivisionsInfo'],$lang['AdminWelcomeTitle']));
	if(isset($_POST['itemID']))
	{
		$style->addRenderRight(new edit_block('reserveddivisions',  'Championships', 
										  'SELECT c_id, c_title FROM ' . CHAMPIONSHIPS_TABLE . ' ORDER BY c_title',
										  'c_id', 'c_title', '', false, $_POST['itemID']));
		$style->addRenderRight(new reserveddivisions($_POST['itemID']));
	}
	else
	{
		$style->addRenderRight(new edit_block('reserveddivisions',  'Championships', 
										  'SELECT c_id, c_title FROM ' . CHAMPIONSHIPS_TABLE . ' ORDER BY c_title',
										  'c_id', 'c_title', '', false));
	}
}
// editing team mmembers?
else if(isset($_GET['action']) && $_GET['action'] == 'teammembers')
{
	include_once("admin/edit_block.php");
	include_once("admin/teammembers.php");
	$style->addRenderRight(new addMessage($lang['AdminEditTeamMembersInfo'],$lang['AdminWelcomeTitle']));
	if(isset($_POST['itemID']))
	{
		$style->addRenderRight(new edit_block('teammembers', 'Teams', 
										  'SELECT t_id, t_name FROM ' . TEAMS_TABLE . ' ORDER BY t_name',
										  't_id', 't_name', '', false, $_POST['itemID']));
		$style->addRenderRight(new teammembers($_POST['itemID']));
	}
	else
	{
	
		$style->addRenderRight(new edit_block('teammembers', 'Teams', 
										  'SELECT t_id, t_name FROM ' . TEAMS_TABLE . ' ORDER BY t_name',
										  't_id', 't_name', '', false));
	}
}
// editing cars?
else if(isset($_GET['action']) && $_GET['action'] == 'cars')
{
    include_once("admin/cars.php");
    $style->addRenderRight(new adminCars());
}
// editing partners?
else if(isset($_GET['action']) && $_GET['action'] == 'partners')
{
	include_once("admin/edit_block.php");
	include_once("admin/edit_partner.php");
	$style->addRenderRight(new addMessage($lang['AdminEditPartnersInfo'],$lang['AdminWelcomeTitle']));
	$style->addRenderRight(new edit_block('partners', 'AdminEditPartners', 
										  'SELECT p_id, p_title FROM ' . PARTNERS_TABLE . ' ORDER BY p_title',
										  'p_id', 'p_title', ''));
	$style->addRenderRight(new edit_partner());
}

// editing licenses?
else if(isset($_GET['action']) && $_GET['action'] == 'licenses')
{
    include_once("admin/licenses.php");
    $style->addRenderRight(new adminLicenses());
}


// mass email sender?
else if(isset($_GET['action']) && $_GET['action'] == 'massemail')
{
	include_once("admin/massemail.php");
	$style->addRenderRight(new addMessage($lang['AdminMassEmailInfo'],$lang['AdminWelcomeTitle']));
	$style->addRenderRight(new massemail());
}

// editing cars?
else if(isset($_GET['action']) && $_GET['action'] == 'carclasses')
{
    include_once("admin/cars.php");
    $style->addRenderRight(new adminCarClasses());
}
// editing logos?
else if(isset($_GET['action']) && $_GET['action'] == 'logo' && isset($_GET['mode']))
{
	include_once("admin/logos.php");
	$style->addRenderRight(new addMessage($lang['AdminEditLogoInfo'],$lang['AdminWelcomeTitle']));
	$style->addRenderRight(new logos($_GET['mode']));
}
else if(isset($_GET['action']) && $_GET['action'] == 'divisions')
{
	include_once("admin/edit_block.php");
	include_once("admin/divisions.php");
	$style->addRenderRight(new addMessage($lang['AdminEditDivisionsInfo'],$lang['AdminWelcomeTitle']));
	
	if(isset($_POST['itemID']))
	{
		$style->addRenderRight(new edit_block('divisions',  'champdiv', 
										  'SELECT l.c_id, l.c_title FROM ' . CHAMPIONSHIPS_TABLE . ' l ORDER BY l.c_title',
										  'c_id', 'c_title', '', false, $_POST['itemID']));
		$style->addRenderRight(new divisions($_POST['itemID']));
	}
	else
	{
	
		$style->addRenderRight(new edit_block('divisions',  'champdiv', 
										  'SELECT l.c_id, l.c_title FROM ' . CHAMPIONSHIPS_TABLE . ' l ORDER BY l.c_title',
										  'c_id', 'c_title', '', false));
	}
}
//honours categories
else if(isset($_GET['action']) && $_GET['action'] == 'honourcats')
{
	include_once("admin/edit_block.php");
	include_once("admin/edit_honours.php");
	$style->addRenderRight(new addMessage($lang['AdminEditHonoursCatagoriesInfo'],$lang['AdminWelcomeTitle']));
	
	if(isset($_POST['itemID']))
	{
		$style->addRenderRight(new edit_block('honourcats',  'champdiv', 
										  'SELECT c_smallTitle, d_title, d_id
										   FROM ' . CHAMPIONSHIPS_TABLE . ', ' . DIVISIONS_TABLE . ' 
										   WHERE c_id = d_champid 
										   ORDER BY c_smallTitle ASC, d_order ASC',
										  'd_id', 'c_smallTitle', 'd_title', false, $_POST['itemID']));
		$style->addRenderRight(new honour_cat($_POST['itemID']));
	}
	else
	{
	
		$style->addRenderRight(new edit_block('honourcats',  'champdiv', 
										  'SELECT c_smallTitle, d_title, d_id
										   FROM ' . CHAMPIONSHIPS_TABLE . ', ' . DIVISIONS_TABLE . ' 
										   WHERE c_id = d_champid 
										   ORDER BY c_smallTitle ASC, d_order ASC',
										  'd_id', 'c_smallTitle', 'd_title', false));
	}
}

//honour winners
else if(isset($_GET['action']) && $_GET['action'] == 'honourents')
{
	include_once("admin/edit_block.php");
	include_once("admin/edit_honours.php");
	$style->addRenderRight(new addMessage($lang['AdminEditHonoursWinnersInfo'],$lang['AdminWelcomeTitle']));
	
	if(isset($_POST['itemID']))
	{
		$style->addRenderRight(new edit_block('honourents',  'AdminEditHonoursCatagories', 
										  'SELECT hc_id, hc_title, c_smallTitle
										   FROM ' . HONOURCATS_TABLE . ', ' . CHAMPIONSHIPS_TABLE . ', ' . DIVISIONS_TABLE . ' 
										   WHERE hc_division = d_id and c_id = d_champid
										   ORDER BY c_lastRace DESC, d_order ASC, hc_order ASC',
										  'hc_id', 'hc_title', 'c_smallTitle', false, $_POST['itemID']));
		$style->addRenderRight(new honour_entry($_POST['itemID']));
	}
	else
	{
	
		$style->addRenderRight(new edit_block('honourents',  'AdminEditHonoursCatagories',  
										  'SELECT hc_id, hc_title, c_smallTitle
										   FROM ' . HONOURCATS_TABLE . ', ' . CHAMPIONSHIPS_TABLE . ', ' . DIVISIONS_TABLE . ' 
										   WHERE hc_division = d_id and c_id = d_champid
										   ORDER BY c_lastRace DESC, d_order ASC, hc_order ASC',
										  'hc_id', 'hc_title', 'c_smallTitle', false));
	}
}
else if(isset($_GET['action']) && $_GET['action'] == 'events')
{
	include_once("admin/edit_block.php");
	include_once("admin/events.php");
	$style->addRenderRight(new addMessage($lang['AdminEditEventsInfo'],$lang['AdminWelcomeTitle']));
	
	if(isset($_POST['itemID']))
	{
		$style->addRenderRight(new edit_block('events',  'Events', 
										  'SELECT c_id, c_title FROM ' . CHAMPIONSHIPS_TABLE . ' ORDER BY c_title',
										  'c_id', 'c_title', '', false, $_POST['itemID']));
		$style->addRenderRight(new events($_POST['itemID']));
	}
	else
	{
		$style->addRenderRight(new edit_block('events',  'Events', 
										  'SELECT c_id, c_title FROM ' . CHAMPIONSHIPS_TABLE . ' ORDER BY c_title',
										  'c_id', 'c_title', '', false));
	}
}
else if(isset($_GET['action']) && $_GET['action'] == 'driverlicenseupgrade')
{
	global $lang;
	include_once("includes/database_query.php");
	$mess = new addMessage($lang['AdminEditEventsInfo'],$lang['AdminWelcomeTitle']);
	
	$query = 'SELECT u_id, pf_drivername, c.l_name as current_license, n.l_minRaces, 
					 n.l_maxPoints, n.l_name, count(*) as racecount,
      				(SELECT COALESCE( SUM(ar_licensepoints) , 0 ) 
					 FROM ' . INCIDENTADMINREVIEW_TABLE . '
       				 WHERE ar_licenseexpiry > NOW() and ar_admin = 0 and ar_driver = u_id) as licensepoints
			  FROM ' . LICENSES_TABLE . ' c, ' . LICENSES_TABLE . ' n, ' . LCUSERS_TABLE . ', ' . 
			  		   RACERESULTS_TABLE . ', ' . DRIVERNAME_TABLE . '
			  WHERE user_id = u_id AND u_license = c.l_id AND n.l_order = c.l_order - 1 AND r_driver = u_id
			  GROUP BY u_id
			  ORDER BY racecount DESC';
	$columns = Array($lang['ChampSignupDriver'], $lang['LicenseCurrent'], $lang['LicenseNext'], $lang['LicenseMinRaces'], 
				$lang['LicenseCurrentRaces'], $lang['LicenseMaxPoints'], $lang['LicenseCurrentPoints']);
	$columnValues = Array('<a href="drivers.php?id={u_id}">{pf_drivername}</a>', '{current_license}', '{l_name}', '{l_minRaces}',
					 '{racecount}', '{l_maxPoints}', '{licensepoints}');
	
	$style->addRenderRight(new database_query($mess,  $query, $columns, $columnValues));
}
// editing bans?
else if(isset($_GET['action']) && $_GET['action'] == 'bans')
{
	include_once("admin/edit_block.php");
	include_once("admin/edit_bans.php");
	$style->addRenderRight(new addMessage($lang['AdminEditBansInfo'],$lang['AdminWelcomeTitle']));
	if(isset($_POST['itemID']))
	{
		$style->addRenderRight(new edit_block('bans', 'Bans', 
										  'SELECT b_id, CONCAT(pf_drivername," - ", COALESCE(date_format(b_endDate, "%M %e, %Y"), "")) as b_title 
										   FROM ' . BANS_TABLE . ', ' . DRIVERNAME_TABLE . ' 
										   WHERE user_id = b_user ORDER BY b_endDate DESC',
										  'b_id', 'b_title', '', true, $_POST['itemID']));
	}
	else
	{
		$style->addRenderRight(new edit_block('bans', 'Bans', 
										  'SELECT b_id, CONCAT(pf_drivername," - ", COALESCE(date_format(b_endDate, "%M %e, %Y"), "")) as b_title 
										   FROM ' . BANS_TABLE . ', ' . DRIVERNAME_TABLE . ' 
										   WHERE user_id = b_user ORDER BY b_endDate DESC',
										  'b_id', 'b_title'));
	}
	$style->addRenderRight(new edit_bans());
}
// editing bans thresholds?
else if(isset($_GET['action']) && $_GET['action'] == 'banthresholds')
{
	include_once("admin/edit_block.php");
	include_once("admin/edit_banthresholds.php");
	$style->addRenderRight(new addMessage($lang['AdminEditBansThresholdsInfo'],$lang['AdminWelcomeTitle']));
	if(isset($_POST['itemID']))
	{
		$style->addRenderRight(new edit_block('banthresholds', 'Bans', 
										  'SELECT CONCAT(bt_license, ",", bt_points) as bt_id, CONCAT(bt_points," pts ", COALESCE(l_name,"")) as bt_title 
										   FROM ' . BANTHRESHOLDS_TABLE . ' LEFT JOIN ' . LICENSES_TABLE . ' ON bt_license = l_id ORDER BY bt_points DESC',
										  'bt_id', 'bt_title', '', true, $_POST['itemID']));
	}
	else
	{
		$style->addRenderRight(new edit_block('banthresholds', 'Bans', 
										  'SELECT CONCAT(bt_license, ",", bt_points) as bt_id, CONCAT(bt_points," pts ", COALESCE(l_name,"")) as bt_title 
										   FROM ' . BANTHRESHOLDS_TABLE . ' LEFT JOIN ' . LICENSES_TABLE . ' ON bt_license = l_id ORDER BY bt_points DESC',
										  'bt_id', 'bt_title'));
	}
	$style->addRenderRight(new edit_banthresholds());
}
else if(isset($_GET['action']) && $_GET['action'] == 'games')
{
    include_once("admin/games.php");
    $style->addRenderRight(new adminGames());
}
else
{
	$style->addRenderRight(new addMessage($lang['AdminWelcome'],$lang['AdminWelcomeTitle']));
}
$style->render();

echo $db->num_queries;
?>